﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Hr.IBLL;
using Newtonsoft.Json;
using Hr.BLLContainer;
using System.Data.Entity;
using Newtonsoft.Json.Linq;
using Hr.BLL;
using Hr.Model;
using System.Web;
using HR.HrHelper;
using Microsoft.Ajax.Utilities;
using System.Web.Helpers;

namespace HR.Areas.liujunjie.Controllers.Process
{
    public class ProcessMangeController : ApiController
    {
        //全局变量，必须的
        //导入BLL层
        protected ProcessService Process = new ProcessService();

        int code = 0;
        string msg = "success";

        #region 获取流程管理页面信息(自己申请的流程)
        /// <summary>
        /// 获取流程管理页面信息(自己申请的流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>
        [HttpGet]
        public JObject SendProcessList(int page, int limit, int fk_User)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.SendProcessList(page,limit,fk_User) }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 流程管理添加下拉框绑定
        /// <summary>
        /// 流程管理添加下拉框绑定
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/17</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject SelectTypeBinding()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.SelectTypeBinding() }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 流程管理添加
        /// <summary>
        /// 流程管理添加
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/17</create>
        /// <updata>null</updata>
        /// 
        [HttpPost]
        public JObject AddProcessList()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            //调用jsonhelper帮助类转换成对象
            Process_List pl = JsonHelper.DeserializeJsonToObject<Process_List>(data["Process"]) as Process_List;

            int fk_User = int.Parse(data["fk_User"].ToString());

            Process_List SelPl = Process.GetModels(n => n.fk_User.Equals(pl.fk_User)).FirstOrDefault();
            msg = "添加成功";
            if (SelPl != null)
            {
                code = 1;
                msg = "角色已存在！";
            }
            else
            {
                if (!Process.AddProcessList(pl, fk_User))
                {
                    code = 1;
                    msg = "添加失败";
                }
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Process.GetModels(n=>true)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 根据Title获取流程管理页面信息(自己申请的流程)
        /// <summary>
        /// 根据Title获取流程管理页面信息(自己申请的流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject TitleSelectProcessList(int page, int limit, int fk_User, string Title)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.TitleSelectProcessList(page,limit,fk_User,Title) }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 流程审核页面(获取需要自己审核的流程)
        /// <summary>
        /// 流程审核页面(获取需要自己审核的流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程审核</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject SendAuditProcessList(int page, int limit, int fk_User)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.SendAuditProcessList(page,limit,fk_User) }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 根据Title获取流程审核页面(获取需要自己审核的流程)
        /// <summary>
        /// 根据Title获取流程审核页面(获取需要自己审核的流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程审核</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject TitleSelectAuditProcessList(int page, int limit, int fk_User, string Title)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.TitleSelectAuditProcessList(page,limit,fk_User,Title) }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 获取流程汇总页面(全部流程)
        /// <summary>
        /// 获取流程汇总页面(全部流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程汇总</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject SummaryProcessList(int page, int limit, int fk_User)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.SummaryProcessList(page,limit,fk_User) }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 根据Title获取流程汇总页面
        /// <summary>
        /// 根据Title获取流程汇总页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程汇总</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject TitleSelectSummaryProcessList(int page, int limit, int fk_User, string Title)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.TitleSelectSummaryProcessList(page,limit,fk_User,Title) }
        };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 流程汇总页面删除功能
        /// <summary>
        /// 流程汇总页面删除功能
        /// </summary>
        /// <param name="id">pk_Process</param>
        /// <returns>流程汇总删除</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject DelSummaryProcessList(int pk_Process)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.DelSummaryProcessList(pk_Process) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 查看页面根据pk_Process查询
        /// <summary>
        /// 查看页面根据pk_Process查询
        /// </summary>
        /// <param name="id">pk_Process</param>
        /// <returns>查看页面</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/17</create>
        /// <updata>null</updata>

        [HttpGet]

        public JObject ProcessDataBinding(int pk_Process)
        {
            //获取头像数据
            List<object> PV = new List<object>();
            foreach (var item in Process.ProcessViewBinding(pk_Process))
            {
                string path = item.GetType().GetProperty("ImagePath1").GetValue(item).ToString();
                var it = new
                {
                    item,
                    ImagePath1 = FileHelper.GetImgInfo(path)
                };
                PV.Add(it);
            }
            //获取头像数据

            string path2 = Process.ProcessDataBinding(pk_Process).GetType().GetProperty("ImagePath1").GetValue(Process.ProcessDataBinding(pk_Process)).ToString();
            var PD = new
            {
                item = Process.ProcessDataBinding(pk_Process),
                ImagePath1 = FileHelper.GetImgInfo(path2)
            };

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",PD },
                {"Twodata",PV }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 流程审核 同意和驳回
        /// <summary>
        /// 流程审核
        /// </summary>
        /// <param name="id">pk_Process</param>
        /// <returns>流程审核</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/18</create>
        /// <updata>null</updata>

        //同意
        [HttpGet]

        public JObject ProcessAgree(int pk_Process, int CheckUser, int StatusID, string Note)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.ProcessAgree(pk_Process,CheckUser,StatusID,Note) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        //驳回
        [HttpGet]

        public JObject RejectedAgree(int pk_Process, int CheckUser, int StatusID, string Note)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.ProcessAgree(pk_Process,CheckUser,StatusID,Note) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 是否禁用按钮
        /// <summary>
        /// 是否禁用按钮
        /// </summary>
        /// <param name="id">pk_Process,CheckUser</param>
        /// <returns>流程审核</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/18</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject IsButton(int pk_Process, int CheckUser)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Process.IsButton(pk_Process,CheckUser) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        #endregion
    }
}
